﻿using PeiYue.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace PeiYue.IRepository.Base
{
    public interface IBaseRepository<TEntity> where TEntity : class
    {

        Task<TEntity> QueryById(object objId);
        Task<TEntity> QueryById(object objId, bool blnUseCache = false);
        Task<List<TEntity>> QueryByIDs(object lstIds);

        Task<TEntity> QueryModel(Expression<Func<TEntity, bool>> whereExpression);
        Task<TEntity> QueryModel(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, TEntity>> selectExpression);

        Task<int> Add(TEntity model);

        Task<int> Add(List<TEntity> listEntity);
        Task<bool> Updates(object obj);

        Task<bool> DeleteById(object id);

        Task<bool> Delete(TEntity model);

        Task<bool> DeleteByIds(object[] ids);
        Task<bool> Deletes(Expression<Func<TEntity, bool>> whereExpression);

        Task<bool> Update(TEntity model);
        Task<bool> Update(TEntity entity, string strWhere);
        Task<bool> Update(object operateAnonymousObjects);
        Task<bool> Update(string strSql, object obj);

        Task<bool> Update(TEntity entity, List<string> lstColumns = null, List<string> lstIgnoreColumns = null, string strWhere = "");

        Task<List<TEntity>> Query();
        Task<List<TEntity>> Query(string strWhere);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds);
        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true);
        Task<List<TEntity>> Query(string strWhere, string strOrderByFileds);

        Task<List<TEntity>> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds);
        Task<List<TEntity>> Query(string strWhere, int intTop, string strOrderByFileds);
        Task<List<TEntity>> QuerySql(string strSql, SugarParameter[] parameters = null);
        Task<DataTable> QueryTable(string strSql, SugarParameter[] parameters = null);

        Task<List<TEntity>> Query(
            Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds);
        Task<List<TEntity>> Query(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds);


        Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null);

        Task<List<TResult>> QueryMuch<T, T2, T3, TResult>(
            Expression<Func<T, T2, T3, object[]>> joinExpression,
            Expression<Func<T, T2, T3, TResult>> selectExpression,
            Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new();

        /// <summary>
        /// 执行sql 分页查询 
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">sql参数</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="orderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        Task<PageModel<T>> SqlQueryPage<T>(string sql, List<SugarParameter> parameters, int pageIndex = 1, int pageSize = 10, string orderByFileds = " id desc ");
        /// <summary>
        /// 执行sql 获取列表 
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">sql参数</param>
        /// <returns></returns>
        Task<List<T>> SqlQueryList<T>(string sql, List<SugarParameter> parameters);
        /// <summary>
        /// 生成全局唯一主键
        /// </summary>
        /// <returns></returns>
        Task<long> GenId();
        /// <summary>
        /// 生成每日唯一主键
        /// </summary>
        /// <returns></returns>
        Task<long> GenIdDaily();
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        Task<int> ExecuteCommand(string sql, object parameters);
        Task<List<TEntity>> SqlQuery(string sql, object obj);
        Task<DataTable> GetDataTableAsync(string sql, object parameters);
        Task<DataSet> GetDataSetAllAsync(string sql, object parameters);
    }
}
